package com.zyl.edthresholdtraining.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyl.edthresholdtraining.common.AppConstant;
import com.zyl.edthresholdtraining.dto.UserInfoDTO;
import com.zyl.edthresholdtraining.entity.UserInfo;
import com.zyl.edthresholdtraining.entity.UserTestAnswer;
import com.zyl.edthresholdtraining.entity.UserTestContent;
import com.zyl.edthresholdtraining.mapper.UserInfoMapper;
import com.zyl.edthresholdtraining.vo.BaseException;
import com.zyl.edthresholdtraining.vo.UserTestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhangyulu
 * @description
 * @date 2020/7/31/031
 */
@Slf4j
@Service
public class UserInfoService extends ServiceImpl<UserInfoMapper, UserInfo> {

    @Autowired
    private UserTestContentService userTestContentService;

    @Autowired
    private UserTestAnswerService userTestAnswerService;

    @Transactional(rollbackFor = Exception.class)
    public String add(UserInfoDTO userInfoDTO) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",userInfoDTO.getName())
                    .eq("age",userInfoDTO.getAge())
                    .eq("sex",userInfoDTO.getSex());
        UserInfo userInfo = getUserInfo(queryWrapper);
        if (userInfo==null) {
            //保存被试信息信息
            userInfo = new UserInfo();
            BeanUtils.copyProperties(userInfoDTO,userInfo);
            baseMapper.insert(userInfo);
            //生成被试测试时需要的组合图片信息
            userTestContentService.makeUpTestContent(userInfo);
        }
        return String.valueOf(userInfo.getId());
    }

    public UserInfo getUserInfo(QueryWrapper<UserInfo> queryWrapper){
        List<UserInfo> userInfos = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(userInfos)) {
            return null;
        }
        return userInfos.get(0);
    }


    /**
    *   前端需要相应的图片URL以及组合图片的名称
    * */
    public List<UserTestVO> getUserTestContent(String userId, HttpServletRequest request) {
        long id = Long.parseLong(userId);
        UserInfo userInfo = baseMapper.selectById(id);
        if (userInfo == null) {
            log.error("输入的编号不存在相应的用户！");
            throw new BaseException("输入的编号不存在相应的用户！");
        }
        //HttpSession session = request.getSession();
        //session.setMaxInactiveInterval(AppConstant.SESSION_EXPIRE_TIME);
        //session.setAttribute(AppConstant.USER_ID,userInfo.getId());
        //session.getAttribute(AppConstant.USER_ID);
        //通过用户输入的编号获取用户需要进行测试的内容
        QueryWrapper<UserTestContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<UserTestContent> userTestContentlist = userTestContentService.list(queryWrapper);
        if (CollectionUtils.isEmpty(userTestContentlist)) {
            log.error("找不到被试对应的测试内容!");
            throw new BaseException("找不到被试对应的测试内容！");
        }
        UserTestContent userTestContent = userTestContentlist.get(0);
        //获取List<UserTestVO>
        List<UserTestVO> userTestVOList = getUserTestVO(userTestContent);
        for (long i = 0; i < id; i++) {
            Collections.shuffle(userTestVOList);
        }
        int index = 1;
        for (UserTestVO userTestVO:userTestVOList) {
            UserTestAnswer userTestAnswer = userTestAnswerService.getNumberResult(userTestVO.getImageUnitName(), userId);
            if (userTestAnswer == null) {
                log.error("找不到图片组合相应的答案！");
                throw new BaseException("找不到图片组合相应的答案！");
            }
            userTestVO.setNumberResult(userTestAnswer.getNumberResult());
            userTestVO.setNumber(index++);
        }
        return userTestVOList;
    }

    private List<UserTestVO> getUserTestVO(UserTestContent userTestContent) {
        List<UserTestVO> userTestVOList = new ArrayList<>(AppConstant.TOTAL);
        String userTestImageName = userTestContent.getUserTestImageName();
        String userTestImage = userTestContent.getUserTestImage();
        String[] imageName = userTestImageName.split(",");
        String[] imagePath = userTestImage.split(",");
        for (int i = 0; i < imageName.length; i++) {
            UserTestVO userTestVO = new UserTestVO();
            userTestVO.setImageUnitName(imageName[i]);
            String imagePaths = imagePath[i];
            String[] split = imagePaths.split(AppConstant.IMAGE_SPLIT);
            userTestVO.setImageUnitPathOne(split[0]);
            userTestVO.setImageUnitPathTwo(split[1]);
            userTestVOList.add(userTestVO);
        }
        return userTestVOList;
    }

}
